package com.example.xmpic.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.example.common.utils.DateUtil;
import com.example.xmpic.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author skygge
 * @date 2019-11-27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：日历控件
 */
public class MonthDateView extends View {
	private static final int NUM_COLUMNS = 7;
	private static final int NUM_ROWS = 6;
	private Paint mPaint;
	private int mCurrYear,mCurrMonth,mCurrDay;
	private int mSelYear,mSelMonth,mSelDay;
	private int mColumnSize,mRowSize;
	private DisplayMetrics mDisplayMetrics;
	private int mDaySize = 14;
	private TextView tv_date;
	private int [][] daysString = new int[6][7];
	private DateClick dateClick;
	private List<Integer> daysHasThingList;

	public MonthDateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		daysHasThingList = new ArrayList<>();
		mDisplayMetrics = getResources().getDisplayMetrics();
		Calendar calendar = Calendar.getInstance();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mCurrYear = calendar.get(Calendar.YEAR);
		mCurrMonth = calendar.get(Calendar.MONTH);
		mCurrDay = calendar.get(Calendar.DATE);
		setSelectYearMonth(mCurrYear, mCurrMonth, mCurrDay);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		initSize();
		int textSize = (int) (mDaySize * mDisplayMetrics.scaledDensity);
		mPaint.setTextSize(textSize);
		String dayString;
		int mMonthDays = DateUtil.getMonthDays(mSelYear, mSelMonth);
		int weekNumber = DateUtil.getFirstDayWeek(mSelYear, mSelMonth);
		Log.d("DateView", "DateView:" + mSelMonth+"月1号周" + weekNumber);
		for(int day = 0;day < mMonthDays; day++){
			dayString = (day + 1) + "";
			int column = (day+weekNumber - 1) % 7;
			int row = (day+weekNumber - 1) / 7;
			daysString[row][column]=day + 1;
			int startX = (int) (mColumnSize * column + (mColumnSize - mPaint.measureText(dayString))/2);
			int startY = (int) (mRowSize * row + mRowSize/2 - (mPaint.ascent() + mPaint.descent())/2);
			drawCircle(dayString, column, row ,canvas, textSize);
			drawRect(row, column,day + 1, canvas, textSize);
			if(dayString.equals(mSelDay+"") ||(daysHasThingList.size() >0 && daysHasThingList.contains(day +1))){
				mPaint.setColor(getResources().getColor(R.color.white));
			}else if(dayString.equals(mCurrDay+"") && mCurrDay != mSelDay && mCurrMonth == mSelMonth){
				mPaint.setColor(getResources().getColor(R.color.main_color)); //正常月，选中其他日期，则今日为红色
			}else{
				mPaint.setColor(getResources().getColor(R.color.text_title));
			}
			canvas.drawText(dayString, startX, startY, mPaint);
		}
		
		if(tv_date != null){
			String title = mSelYear + "/" + (mSelMonth + 1);
			tv_date.setText(title);
		}
	}

	//绘制选中日期背景色圆形
	private void drawCircle(String dayString, int column, int row,  Canvas canvas, int textSize){
		if(dayString.equals(String.valueOf(mSelDay))){
			int startRecX = mColumnSize * column;
			int startRecY = mRowSize * row;
			int endRecX = startRecX + mColumnSize/2;
			int endRecY = startRecY + mRowSize/2;
			mPaint.setColor(getResources().getColor(R.color.text_error));
			int radius = mRowSize/2-textSize/3;
			canvas.drawCircle(endRecX, endRecY, radius, mPaint);
		}
	}

	//绘制背景色矩形
	private void drawRect(int row, int column, int day, Canvas canvas, int textSize){
		if(daysHasThingList != null && daysHasThingList.size() >0){
			if (day == mSelDay){
				return;
			}
			if(!daysHasThingList.contains(day)){
				mPaint.setColor(getResources().getColor(R.color.empty_history));
			}else {
				mPaint.setColor(getResources().getColor(R.color.main_color));
			}
		}else {
			if (day == mSelDay){
				return;
			}
			mPaint.setColor(getResources().getColor(R.color.empty_history));
		}
		int margin = textSize/2;
		int startRecX = mColumnSize * column + textSize/2 + margin/2;
		int startRecY = mRowSize * row + margin;
		int endRecX = startRecX + mRowSize - margin*2;
		int endRecY = startRecY + mRowSize - margin*2;
		canvas.drawRect(startRecX, startRecY, endRecX, endRecY, mPaint);
	}
	@Override
	public boolean performClick() {
		return super.performClick();
	}

	private int downX = 0,downY = 0;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int eventCode=  event.getAction();
		switch(eventCode){
		case MotionEvent.ACTION_DOWN:
			downX = (int) event.getX();
			downY = (int) event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			int upX = (int) event.getX();
			int upY = (int) event.getY();
			if(Math.abs(upX-downX) < 10 && Math.abs(upY - downY) < 10){//点击事件
				performClick();
				doClickAction((upX + downX)/2,(upY + downY)/2);
			}
			break;
		}
		return true;
	}

	/**
	 * 初始化列宽行高
	 */
	private void initSize(){
		mColumnSize = getWidth() / NUM_COLUMNS;
		mRowSize = getHeight() / NUM_ROWS;
	}
	
	/**
	 * 设置年月
	 * @param year
	 * @param month
	 */
	private void setSelectYearMonth(int year,int month,int day){
		mSelYear = year;
		mSelMonth = month;
		mSelDay = day;
	}
	/**
	 * 执行点击事件
	 * @param x
	 * @param y
	 */
	private void doClickAction(int x,int y){
		int row = y / mRowSize;
		int column = x / mColumnSize;
		setSelectYearMonth(mSelYear,mSelMonth,daysString[row][column]);
		invalidate();
		//执行activity发送过来的点击处理事件
		if(dateClick != null){
			dateClick.onClickOnDate();
		}
	}

	/**
	 * 左点击，日历向后翻页
	 */
	public void onLeftClick(){
		int year = mSelYear;
		int month = mSelMonth;
		int day = mSelDay;
		if(month == 0){//若果是1月份，则变成12月份
			year = mSelYear-1;
			month = 11;
		}else if(DateUtil.getMonthDays(year, month) == day){
			//如果当前日期为该月最后一点，当向前推的时候，就需要改变选中的日期
			month = month-1;
			day = DateUtil.getMonthDays(year, month);
		}else{
			month = month-1;
		}
		daysHasThingList.clear();
		setSelectYearMonth(year,month,day);
		invalidate();
	}
	
	/**
	 * 右点击，日历向前翻页
	 */
	public void onRightClick(){
		int year = mSelYear;
		int month = mSelMonth;
		int day = mSelDay;
		if(month == 11){//若果是12月份，则变成1月份
			year = mSelYear+1;
			month = 0;
		}else if(DateUtil.getMonthDays(year, month) == day){
			//如果当前日期为该月最后一点，当向前推的时候，就需要改变选中的日期
			month = month + 1;
			day = DateUtil.getMonthDays(year, month);
		}else{
			month = month + 1;
		}
		daysHasThingList.clear();
		setSelectYearMonth(year,month,day);
		invalidate();
	}
	
	/**
	 * 获取选择的年份
	 * @return
	 */
	public int getmSelYear() {
		return mSelYear;
	}
	/**
	 * 获取选择的月份
	 * @return
	 */
	public int getmSelMonth() {
		return mSelMonth;
	}
	/**
	 * 获取选择的日期
	 */
	public int getmSelDay() {
		return this.mSelDay;
	}

	/**
	 * 日期的大小，默认18sp
	 * @param mDaySize
	 */
	public void setmDaySize(int mDaySize) {
		this.mDaySize = mDaySize;
	}
	/**
	 * 设置显示当前日期的控件
	 * @param tv_date
	 * 		显示日期
	 * 		显示周
	 */
	public void setTextView(TextView tv_date){
		this.tv_date = tv_date;
		invalidate();
	}

	/**
	 * 设置事务天数
	 * @param daysHasThingList
	 */
	public void setDaysHasThingList(List<Integer> daysHasThingList) {
		this.daysHasThingList = daysHasThingList;
		invalidate();
	}

	/**
	 * 设置日期的点击回调事件
	 * @author shiwei.deng
	 *
	 */
	public interface DateClick{
		void onClickOnDate();
	}

	/**
	 * 设置日期点击事件
	 * @param dateClick
	 */
	public void setDateClick(DateClick dateClick) {
		this.dateClick = dateClick;
	}
	
	/**
	 * 跳转至今天
	 */
	public void setTodayToView(){
		setSelectYearMonth(mCurrYear,mCurrMonth,mCurrDay);
		invalidate();
	}

	/**
	 * 跳转至今天
	 */
	public void setToday(Calendar calendar){
		mCurrYear = calendar.get(Calendar.YEAR);
		mCurrMonth = calendar.get(Calendar.MONTH);
		mCurrDay = calendar.get(Calendar.DATE);
		setSelectYearMonth(mCurrYear,mCurrMonth,mCurrDay);
		invalidate();
	}
}
